﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NCrawler.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ZCrawler
{
    public class ZRuleJson : ZBasicRule
    {
        public ZRuleJson() { }
        public ZRuleJson(string fn) : base(fn) { }
        public override string Process<T>(T source)
        {
            if (EOF) return "";
            string result = "";
            try
            {
                JObject jObj = JObject.Parse(source as string);
                JToken token = jObj.SelectToken(searchFieldTree);
                result = token.ToString();
                EOF = true;
            }
            catch (Exception) { EOF = true; }
            return result;
        }
        public override string Description()
        {
            return "<Rule><Json>{0}</Json></Rule>".FormatWith(searchFieldTree);
        }
    }
}
